package com.haixiaoke.saas.maintenance.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.constant.MaintenanceConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.Estate;
import com.haixiaoke.saas.house.service.IEstateService;
import com.haixiaoke.saas.maintenance.domain.MaintenanceItem;
import com.haixiaoke.saas.maintenance.mapper.MaintenanceItemMapper;
import com.haixiaoke.saas.maintenance.service.IMaintenanceItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 维修项目信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-09-03
 */
@Service
public class MaintenanceItemServiceImpl implements IMaintenanceItemService {
    @Autowired
    private MaintenanceItemMapper maintenanceItemMapper;
    @Autowired
    private IEstateService estateService;

    /**
     * 查询维修项目信息
     *
     * @param maintenanceItemId 维修项目信息主键
     * @return 维修项目信息
     */
    @Override
    public MaintenanceItem selectMaintenanceItemByMaintenanceItemId(Long maintenanceItemId) {
        return maintenanceItemMapper.selectMaintenanceItemByMaintenanceItemId(maintenanceItemId);
    }

    /**
     * 查询维修项目信息列表
     *
     * @param maintenanceItem 维修项目信息
     * @return 维修项目信息
     */
    @Override
    public List<MaintenanceItem> selectMaintenanceItemList(MaintenanceItem maintenanceItem) {
        return maintenanceItemMapper.selectMaintenanceItemList(maintenanceItem);
    }

    /**
     * 新增维修项目信息
     *
     * @param maintenanceItem 维修项目信息
     * @return 结果
     */
    @Override
    public int insertMaintenanceItem(MaintenanceItem maintenanceItem) {
        maintenanceItem.setCreateTime(DateUtils.getNowDate());
        return maintenanceItemMapper.insertMaintenanceItem(maintenanceItem);
    }

    /**
     * 修改维修项目信息
     *
     * @param maintenanceItem 维修项目信息
     * @return 结果
     */
    @Override
    public int updateMaintenanceItem(MaintenanceItem maintenanceItem) {
        maintenanceItem.setUpdateTime(DateUtils.getNowDate());
        return maintenanceItemMapper.updateMaintenanceItem(maintenanceItem);
    }

    /**
     * 批量删除维修项目信息
     *
     * @param maintenanceItemIds 需要删除的维修项目信息主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceItemByMaintenanceItemIds(Long[] maintenanceItemIds) {
        return maintenanceItemMapper.deleteMaintenanceItemByMaintenanceItemIds(maintenanceItemIds);
    }

    /**
     * 删除维修项目信息信息
     *
     * @param maintenanceItemId 维修项目信息主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceItemByMaintenanceItemId(Long maintenanceItemId) {
        return maintenanceItemMapper.deleteMaintenanceItemByMaintenanceItemId(maintenanceItemId);
    }

    @Override
    public int addMaintenanceItem(MaintenanceItem maintenanceItem) {
        MaintenanceItem sqlMaintenanceItem = maintenanceItemMapper.selectMaintenanceItemByTenantIdAndMaintenanceItemName(maintenanceItem.getTenantId(), maintenanceItem.getMaintenanceItemName());
        if (ObjectUtil.isNotEmpty(sqlMaintenanceItem)) {
            throw new ServiceException("维修项目名称已存在");
        }

        if (MaintenanceConstants.PROJECT_TYPE_ASSIGN.equals(maintenanceItem.getProjectType())) {
            Estate estate = estateService.selectEstateByEstateIdInfo(maintenanceItem.getEstateId());
            if (ObjectUtil.isEmpty(estate)) {
                throw new ServiceException("楼盘不存在");
            }
            maintenanceItem.setEstateName(estate.getEstateName());
        }
        maintenanceItem.setCreateTime(DateUtils.getNowDate());
        return maintenanceItemMapper.insertMaintenanceItem(maintenanceItem);
    }

    @Override
    public int editMaintenanceItem(MaintenanceItem maintenanceItem) {
        MaintenanceItem sqlMaintenanceItem = maintenanceItemMapper.selectMaintenanceItemByTenantIdAndMaintenanceItemName(maintenanceItem.getTenantId(), maintenanceItem.getMaintenanceItemName());
        if (ObjectUtil.isNotEmpty(sqlMaintenanceItem)) {
            if (!sqlMaintenanceItem.getMaintenanceItemId().equals(maintenanceItem.getMaintenanceItemId())) {
                throw new ServiceException("维修项目名称已存在");
            }
        }

        if (MaintenanceConstants.PROJECT_TYPE_ASSIGN.equals(maintenanceItem.getProjectType())) {
            Estate estate = estateService.selectEstateByEstateIdInfo(maintenanceItem.getEstateId());
            if (ObjectUtil.isEmpty(estate)) {
                throw new ServiceException("楼盘不存在");
            }
            maintenanceItem.setEstateName(estate.getEstateName());
        } else {
            maintenanceItem.setEstateName("");
            maintenanceItem.setEstateId("");
        }
        maintenanceItem.setUpdateTime(DateUtils.getNowDate());
        return maintenanceItemMapper.updateMaintenanceItem(maintenanceItem);
    }

    @Override
    public List<MaintenanceItem> selectMaintenanceItemInfo(MaintenanceItem maintenanceItem) {
        return maintenanceItemMapper.selectMaintenanceItemInfo(maintenanceItem);
    }
}
